package com.pay.game.central.system.mapper;

import com.pay.game.central.system.bean.Role;
import com.pay.game.central.system.bean.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface UserMapper {

    @Insert("insert into sys_user(id,username,name,password,tel,alipay,wechat,pay_code,pid,level) " +
            "values(#{id},#{username},#{name},#{password},#{tel},#{alipay}," +
            "   #{wechat},#{pay_code},#{pid},#{level,jdbcType=INTEGER})")
    void addUser(User user);

    @Insert("insert into sys_role_user(user_id,role_id) values (#{userId}, #{roleId,jdbcType=INTEGER})")
    void addRole(@Param("userId") String userId, @Param("roleId") int roleId);

    @Update("update sys_user set online=#{state,jdbcType=BOOLEAN} where id=#{id}")
    void setUserOnlineState(@Param("state") boolean state,@Param("id") String id);

    @Update("update sys_user set pid=#{pid} where id=#{id}")
    void setPid(@Param("id") String id,@Param("pid") String pid);

    @Update("update sys_user set interest_rate=#{rate,jdbcType=FLOAT} where id=#{id}")
    void setInterestRate(@Param("rate") float interestRate,@Param("id") String id);

    @Update("update sys_user set score=#{score,jdbcType=INTEGER} where id=#{id}")
    void setScore(@Param("id") String id, @Param("score") int score);

    @Update({"<script>",
                "update sys_user " +
                "<set> " +
                "  tel=#{tel}," +
                "  alipay=#{alipay}," +
                "  wechat=#{wechat}," +
                "   <if test=\"password!='' and password!=null\"> " +
                "       password=#{password}," +
                "   </if>" +
                "   <if test=\"name!='' and name!=null\"> " +
                "       name=#{name}," +
                "   </if>" +
                "</set>" +
                "where id=#{id}",
            "</script>"})
    void updateUserInfo(User user);

    @Update("update sys_user set `pause`=#{pause,jdbcType=TINYINT} where id=#{id}")
    void updateUserPause(@Param("id") String id, @Param("pause") int pause);

    @Delete("update sys_user set is_del=true where id like concat(#{id},'%')")
    void deleteUserById(@Param("id") String id);

    @Delete("delete from sys_role_user where user_id=#{userId} and role_id=#{roleId}")
    void removeRole(@Param("userId") String userId, @Param("roleId") String roleId);

    @Delete("delete from sys_role_user where user_id=#{userId}")
    void removeUserAllRoles(@Param("userId") String userId);

    @Select("select * from sys_user where username=#{username} and password=#{password}")
    User getUserByNamePwd(@Param("username")String username,@Param("password")String password);

    @Select("select * from sys_user where id=#{id}")
    User findUserById(@Param("id") String id);

    @Select("select child.interest_rate rate, father.interest_rate father_rate, " +
            "child.name user_name, father.id p_user_id, father.name p_user_name " +
            "from sys_user child " +
            "left join sys_user father on child.pid = father.id where child.id=#{id}")
    Map<String,Object> getRateAndFatherRate(@Param("id") String id);

    @Select("select id,password,username from sys_user where id=#{id}")
    User geValidateInfoById(@Param("id") String id);

    @Select("select name from sys_user where id=#{id}")
    User getNameById(@Param("id") String id);

    @Select("select * from sys_user where username=#{username}")
    User findUserByUserName(@Param("username") String username);

    @Select("select sr.* from sys_role sr join sys_role_user sru on sru.role_id=sr.id where sru.user_id=#{userId}")
    List<Role> listRole(@Param("userId") String userId);

    @Select("<script> " +
            "   select * from sys_user where is_del=0 " +
            "   <if test=\"username != null and username != ''\">" +
            "       and username like concat(#{username},'%')" +
            "   </if>" +
            "   <if test=\"name != null and name != ''\">" +
            "       and name like concat(#{name},'%')" +
            "   </if>" +
            "   <if test=\"tel != null and tel != ''\">" +
            "       and tel like concat(#{tel},'%')" +
            "   </if>" +
            "   <if test=\"rootId != null and rootId != ''\">" +
            "       and id like concat(#{rootId},'%')" +
            "   </if>" +
            "</script>")
    List<User> list(@Param("username")String username,@Param("name")String name,@Param("tel")String tel,@Param("rootId")String rootId);

    @Select("select id,username from sys_user where level=1 and is_del=0")
    List<Map<String, Object>> listRoot();


    @Select("<script>" +
            "   select id from sys_user" +
            "   where " +
            "       <if test=\"pid != null and pid != ''\">" +
            "           pid=#{pid} " +
            "       </if>" +
            "       <if test=\"pid == null or pid == ''\">" +
            "           pid is null or pid=''" +
            "       </if>" +
            "   order by id desc limit 1" +
            "</script>")
    String getChildrenLastId(@Param("pid") String pid);

    @Select("<script>" +
            "select su.id, su.name, su.score score_limit, su.interest_rate, su.pause, psu.interest_rate p_rate, " +
            "sum(ps.used_score) used_score, " +
            "sum(pg.loop_times) order_size_looped, sum(pg.done_times) order_size_done, " +
            "su.order_limit order_size_limit  " +
            "from sys_user su " +
            "left join pdd_store ps on ps.user_id = su.id " +
            "left join pdd_goods pg on pg.store_id = ps.id " +
            "left join sys_user psu on su.pid = psu.id " +
            "where su.is_del = 0 " +
            "   <if test=\"id != null and pid != ''\">" +
            "       and su.id like concat(#{id},'%')" +
            "   </if>" +
            "group by su.id "+
            "</script>")
    List<Map> getManageInfo(String id);

    @Update("update sys_user set " +
            "score=#{score,jdbcType=INTEGER}, interest_rate=#{interest_rate, jdbcType=FLOAT}," +
            "order_limit = #{order_limit,jdbcType=INTEGER} " +
            "where id=#{id}")
    void editManageInfo(String id, int score, float interest_rate, int order_limit);
}
